import {defineStore} from 'pinia';
import {getDictionaryData,getUploadUrl} from '@/api/common.js'
export default defineStore('useBaseStore',{
	id: ' app-base',
	state: () => ({
		dictionaryList: {},
		comUploadUrl:uni.getStorageSync('comUploadUrl') || ''
	}),
	getters: {
		getDictionaryList() {
			return this.dictionaryList
		},
		getRelationData() {
			return this.relationData
		},
	},
	actions: {
		async getUploadUrlApi(){
			if(this.comUploadUrl){
				return this.comUploadUrl
			}else{
				let res= await getUploadUrl()
				this.setUploadUrl(res.result)
				return res.result
			}
		},
		setUploadUrl(url){
			this.comUploadUrl = url
			uni.setStorageSync('comUploadUrl', url)
		},
		setDictionaryList(v, dictionaryList) {
			this.dictionaryList[v] = dictionaryList || []
		},
		getDictionaryDataAll(data) {
			return new Promise((resolve, reject) => {
				if (this.dictionaryList[data]) {
					resolve(this.dictionaryList[data])
				} else {
					getDictionaryData({
						dictCodes: data
					}).then(res => {
						this.setDictionaryList(data, res.result[0].dictItemModelList ? res
							.result[0].dictItemModelList : [])
						resolve(res.result[0].dictItemModelList ? res.result[0]
							.dictItemModelList : [])
					}).catch(error => {
						reject(error)
					})
				}
			})
		},
	},
});